<?php
/**
 + Created by:	Webit Solution
 * Company:		Webit Solution
 + Contact:		www.webitsolution.com , info@webitsolution.com
				www.wrecruit.com, developer@wrecruit.com
 * Created on:	Mar 21, 2012
 ^
 + Project: 		WRecruit 
 * File Name:	module/wrecruitstates.php
 ^ 
 * Description: Module for WRecruit
 ^ 
 * History:		1.0.0 - Mar 21, 2012
 ^ 
 */

defined('_JEXEC') or die('Restricted access');

jimport('joomla.plugin.plugin');
 
//The Content plugin Loadmodule
class plgContentWRecruitCities extends JPlugin
{
	// for joomla 1.5
	public function onPrepareContent( &$row, &$params, $page=0 )  {
                if ( JString::strpos( $row->text, 'wrecruitcities' ) === false ) {
                        return true;
		}
              // expression to search for
                $regex = '/{wrecruitcities\s*.*?}/i';
                if ( !$this->params->get( 'enabled', 1 ) ) {
                        $row->text = preg_replace( $regex, '', $row->text );
                        return true;
                }
                preg_match_all( $regex, $row->text, $matches );
                $count = count( $matches[0] );
                if ( $count ) {
                        // Get plugin parameters
                        $style = $this->params->def( 'style', -2 );
                        $this->_process( $row, $matches, $count, $regex, $style );
                }
	}
	   // for joomla 1.6			
       public function onContentPrepare( $context, &$row, &$params, $page=0 ){
                if ( JString::strpos( $row->text, 'wrecruitcities' ) === false ) {
                        return true;
		}
              // expression to search for
                $regex = '/{wrecruitcities\s*.*?}/i';
                if ( !$this->params->get( 'enabled', 1 ) ) {
                        $row->text = preg_replace( $regex, '', $row->text );
                        return true;
                }
                preg_match_all( $regex, $row->text, $matches );
                $count = count( $matches[0] );
                if ( $count ) {
                        // Get plugin parameters
                        $style = $this->params->def( 'style', -2 );
                        $this->_process( $row, $matches, $count, $regex, $style );
                }
	}	
        protected function _process( &$row, &$matches, $count, $regex, $style )  {
                for ( $i=0; $i < $count; $i++ )
                {
                        $load = str_replace( 'wrecruitcities', '', $matches[0][$i] );
                        $load = str_replace( '{', '', $load );
                        $load = str_replace( '}', '', $load );
                        $load = trim( $load );
 
                        $modules       = $this->_load( $load, $style );
                        $row->text         = preg_replace( '{'. $matches[0][$i] .'}', $modules, $row->text );
                }
                $row->text = preg_replace( $regex, '', $row->text );
        }
        protected function _load( $position, $style=-2 ) {
                $document      = &JFactory::getDocument();
                $renderer      = $document->loadRenderer('module');
                $params                = array('style'=>$style);
		$noofjobs = $this->params->get('noofjobs', 7);
		$jobsincity = $this->params->get('jobsincity', 1);
		$allcity = $this->params->get('allcity', 0);
		$defaultcountry = $this->params->get('defaultcountry', 0);
		$shtitle = $this->params->get('shtitle');
		$title = $this->params->get('title');
		$theme = $this->params->get('theme', 1);
		$colperrow = $this->params->get('colperrow',3);
		$separator= $this->params->get('separator');

		//sce
		$sliding= $this->params->get('sliding','1');
		$consecutivesliding= $this->params->get('consecutivesliding','3');
		$slidingdirection= $this->params->get('slidingdirection','1'); // 0 = left  , 1=up
		//sce


		$colwidth = Round(100/$colperrow,1);
		$colwidth = $colwidth.'%';
		//scs				
		if($this->params->get('Itemid')) $itemid = $this->params->get('Itemid');			
		else  $itemid =  JRequest::getVar('Itemid');
		//sce
		/*
		$db =& JFactory::getDBO();

		$query = "SELECT * FROM ". $db->nameQuote('#__wrecruit_config')." 
		WHERE configname = 'theme' OR configname = 'date_format' OR configname = 'defaultcountry'  ";
		$db->setQuery($query);
		$configs = $db->loadObjectList();
		foreach($configs AS $config){
			if ($config->configname == 'theme')$css = $config->configvalue;
			if ($config->configname == 'date_format')$dateformat = $config->configvalue;
			if ($config->configname == 'defaultcountry')$default_country = $config->configvalue;
		}	
		if ($theme == 1){ // wrecruit theme
			$trclass = array("odd", "even");
			if ($css == 'templatetheme.css') $trclass = array("sectiontableentry1", "sectiontableentry2");
			$document =& JFactory::getDocument();
			$document->addStyleSheet('components/com_wrecruit/css/'.$css);
		}elseif ($theme == 2){ // template theme
			$trclass = array("sectiontableentry1", "sectiontableentry2");
		}else 	$trclass = array("", ""); //no theme

	
		$curdate = date('Y-m-d H:i:s');
		$inquery =  " (SELECT COUNT(job.id) from ".$db->nameQuote('#__wrecruit_jobs')."  AS job WHERE city.code = job.city AND job.status = 1 AND job.jobstatus BETWEEN 1 AND 5 AND job.startpublishing <= " . $db->Quote($curdate) . " AND job.stoppublishing >= " . $db->Quote($curdate) . " ) as jobsbycity";                                

		$query =  "SELECT  DISTINCT city.id, city.name,city.countrycode,city.statecode,city.countycode, city.code,  ";
		$query .= $inquery;
		$query .=  " FROM ".$db->nameQuote('#__wrecruit_cities')." AS city 
			LEFT JOIN ".$db->nameQuote('#__wrecruit_jobs')."  AS job On city.code = job.city 
		WHERE city.enabled =".$db->quote(Y) ;
		if ($defaultcountry) $query .= " AND city.countrycode = ".$db->quote($default_country);
		$query .=  " ORDER BY city.name ";
		//echo $query;
		$query2 = "SELECT job.state, job.country, job.county,job.city, count(job.id) AS jobsbycity FROM ".$db->nameQuote('#__wrecruit_jobs')."  AS job WHERE job.status = 1 AND job.startpublishing <= " . $db->Quote($curdate) . " AND job.stoppublishing >= " . $db->Quote($curdate) . " AND job.city != '' " ;                           
		if ($defaultcountry) $query2 .= " AND job.country = ".$db->quote($default_country);
		$query2 .= " AND NOT EXISTS ( SELECT id FROM ".$db->nameQuote('#__wrecruit_cities')." WHERE code = job.city)  ";
		$query2 .= " GROUP BY job.city";
		//echo $query2;



		$db->setQuery($query);
		* 
		*/
			/** scs */
			$componentAdminPath = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_wrecruit';
			$componentPath =  'components' . DS . 'com_wrecruit';
			require_once $componentPath . DS . 'models' . DS . 'mpwrecruit.php';
			$config = array( 'table_path' => $componentAdminPath . DS . 'tables');
			$model = new WRecruitModelMpWRecruit($config);
			$result = $model->getJobsCity($noofjobs,$theme);
			$cities = $result[0];
			$trclass = $result[1];	
			$dateformat = $result[2];
			$cities2 = $result[3];
				
			/** sce */
		
		    $contents = '<table cellpadding="0" cellspacing="0" border="0" width="100%" class="contentpane">';
			$isodd = 1;
			$count = 1;
			if ($shtitle == 1){
					$contents .=  '<tr class="'.$trclass[$isodd].'">';
				$contents .=  '<td colspan="'.$colperrow.'">';
				$contents .=  '<h2><u>'.$title.'</u></h2>';	
				if ($separator == 1) $contents .= '<hr style="border:dashed #C0C0C0; border-width:1px 0 0 0; height:0;line-height:0px;font-size:0;margin:0;padding:0;">';
				$contents .= '</td>';
				$contents .= '</tr>';
			}	
		if ($cities) { 
			foreach ($cities as $city) {
				if ($allcity == 0){ // show only those categories who have jobs
					if($city->jobsbycity > 0 ) $printrecord = 1; else $printrecord = 0;
				}else $printrecord = 1;
				if ($noofjobs != -1){
					if ($count >= $noofjobs ) $printrecord = 0;
				}
				if ($printrecord == 1){	
					if ($count == 1){
						$isodd = 1 - $isodd;
						$contents .= '<tr class="'.$trclass[$isodd].'">';
					}
					$lnks = 'index.php?option=com_wrecruit&c=wrecruit&view=jobseeker&layout=listnewestjobs&country='. $city->countrycode .'&state='. $city->statecode .'&county='. $city->countycode .'&city='. $city->code .'&lt=1&Itemid='.$itemid; 
					$lnks = JRoute::_($lnks);
					$contents .= '<td width="'.$colwidth.'"><a href="'.$lnks.'" >'.$city->name;
							if ($jobsincity == 1) $contents .= ' ('. $city->jobsbycity.')';
							$contents .= '</a></td>';
					if ($count == $colperrow){
						$contents .= '</tr><tr class="'.$trclass[$isodd].'">';
						$count = 0;
					}
					$count++;			
				}	
		    	}
		}
			if ($cities2) { 
				foreach ($cities2 as $city) {
					if ($allcity == 0){ 
						if($city->jobsbycity > 0 ) $printrecord = 1; else $printrecord = 0;
					}else $printrecord = 1;
					if ($noofjobs != -1){
						if ($count >= $noofjobs ) $printrecord = 0;
					}
					if ($printrecord == 1){	
						$isodd = 1 - $isodd;
						$lnks = 'index.php?option=com_wrecruit&c=wrecruit&view=jobseeker&layout=listnewestjobs&country='.$city->country.'&state='. $city->state.'&county='. $city->county.'&city='. $city->city.'&lt=1&Itemid='.$itemid; 
						$lnks = JRoute::_($lnks);
						$contents .= '<td width="'.$colwidth.'"><a href="'.$lnks.'" >'.$city->city;
									if ($jobsincity == 1) $contents .= ' ('. $city->jobsbycity.')';
								$contents .= '</a></td>';
			
						if ($count == $colperrow){
							$contents .= '</tr><tr class="'.$trclass[$isodd].'">';
							$count = 0;
						}
						$count++;			
					}	
			    	}
			}
			 if ($count-1 < $colperrow){
				for ($i = $count; $i <= $colperrow; $i++){
				    $contents .= '<td></td>';
				}
				$contents .= '</tr>';
			}

	
			$contents .= '</table>';
			//scs		
			if ($sliding == 1) {                    
				
				if($slidingdirection == 1 ){
					for ($a = 0; $a < $consecutivesliding; $a++){ 	$contents .= $contents; }
					$contents =  '<marquee  direction="up" scrollamount="2" onmouseover="this.stop();" onmouseout="this.start()";>'.$contents.'</marquee>';
					$contents = $contents.'<br clear="all">';
				}else{
					$tcontents = '<table cellpadding="0" cellspacing="0" border="1" width="100%" class="contentpane"> <tr>';
					for ($a = 0; $a < $consecutivesliding; $a++){ $scontents = '<td>'.$contents.'</td>'; }
					$contents = $tcontents.$scontents.'</tr></table>';
					$contents =  '<marquee  scrollamount="2" onmouseover="this.stop();" onmouseout="this.start()";>'.$contents.'</marquee>';
				}	
			}
			//sce
			
			return $contents;
	}	
	
}
?>

